﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Starfire3d.Data.States.UnitStates;
using Starfire3d.Managers;

namespace Starfire3d.Data.States.GameStates
{
    public class GameScreen : BaseState<GameManager>
    {
        public override void Enter(GameManager entity)
        {
            System.Console.WindowHeight = 62;
            System.Console.BufferHeight = 62;
            System.Console.WindowWidth = 120;
            System.Console.BufferWidth = 120;

            DataStore.Clear();

            Unit player = null;

            // Create playing pieces.
            for (int i = 0; i < entity.NumOfPlayers; i++)
            {
                player = new Unit();

                player.Entity_Name = (i + 1).ToString();
                player.Data.Add("X", RandomGenerator.Rand(50) - 1, true);
                player.Data.Add("Y", RandomGenerator.Rand(50) - 1, true);
                //player.Data.Add("PD%", 30, true);

                DataStore.Add(player);
            }

            //player = new Unit();
            //player.Entity_Name = "@";
            //player.Data.Add("X", RandomGenerator.Rand(50) - 1, true);
            //player.Data.Add("Y", RandomGenerator.Rand(50) - 1, true);
            //player.Data.Add("PD%", 60, true);
            //DataStore.Add(player);
            
            System.Console.SetCursorPosition(0, 0);

            for (int a = 0; a < 50; a++)
            {
                for (int b = 0; b < 50; b++)
                    System.Console.Write('.');
                System.Console.Write('\n');
            }
        }

        public override void Execute(GameManager entity)
        {
            Thread.Sleep(500);
            DrawBoard();

            foreach (Unit unit in DataStore.GetAll<Unit>())
                unit.Data.Add("INIT", RandomGenerator.Rand(100), true);

            IEnumerable<GameObject> unitList = DataStore.GetAll<GameObject>().OrderBy(gObject => gObject.Data.GetInt("INIT"));

            foreach (GameObject unit in unitList)
            {
                if (unit.Update())
                    DataStore.Remove(unit.Entity_Name, unit is Unit);
            }

            DrawStats();

            if (DataStore.GetAll<GameObject>().Length <= 1)
                entity.StateMachine.ChangeState(new OptionsScreen());

            MovementManager.NewMovementPulse();
        }

        public override void Exit(GameManager entity)
        {
        }

        private void DrawBoard()
        {
            foreach (Ordinance missile in DataStore.GetAll<Ordinance>())
            {
                if (missile.Data.GetInt("PX") != -1)
                {
                    System.Console.SetCursorPosition(missile.Data.GetInt("PX"), missile.Data.GetInt("PY"));
                    System.Console.Write(".");
                }
                System.Console.SetCursorPosition(missile.Data.GetInt("X"), missile.Data.GetInt("Y"));
                System.Console.Write("*");
            }
            foreach (Unit unit in DataStore.GetAll<Unit>())
            {
                if (unit.Data.GetInt("PX") != -1)
                {
                    System.Console.SetCursorPosition(unit.Data.GetInt("PX"), unit.Data.GetInt("PY"));
                    System.Console.Write(".");
                }
                System.Console.SetCursorPosition(unit.Data.GetInt("X"), unit.Data.GetInt("Y"));
                System.Console.Write(unit.Entity_Name);
            }

            System.Console.SetCursorPosition(52, 0);
            System.Console.Write("Players:");
            System.Console.SetCursorPosition(52, 1);
            System.Console.Write(DataStore.GetAll<Unit>().Length.ToString());
        }

        private void DrawStats()
        {
            int col = 4;

            System.Console.SetCursorPosition(52, 3);
            System.Console.Write("Stats:");

            foreach (Unit unit in DataStore.GetAll<Unit>())
            {
                System.Console.SetCursorPosition(0, 48 + col);
                System.Console.Write(" " + unit.Entity_Name + " : ");
                System.Console.Write("                                                                                                            ");
                System.Console.SetCursorPosition(5, 48 + col);
                System.Console.Write(unit.Sheet.Details.ToString() + " [" + unit.Sheet.Details.MaxSpeed.ToString() + "]");

                System.Console.SetCursorPosition(52, col);
                System.Console.Write("                            ");
                System.Console.SetCursorPosition(52, col++);
                System.Console.Write(unit.ToString());
            }
            foreach (KeyValuePair<string, GameObject> pair in DataStore.DeadStore)
            {
                System.Console.SetCursorPosition(0, 48 + col);
                System.Console.Write("                                                                                                                 ");
                System.Console.SetCursorPosition(52, col);
                System.Console.Write("                            ");
                System.Console.SetCursorPosition(52, col++);
                System.Console.Write(pair.Value.ToString());
            }
        }
    }
}
